define([
    'text!sysmonitoring/datarecovery/templates/Search.html',
], function(SearchTpl) {

    return fish.View.extend({
        template: fish.compile(SearchTpl),
        i18nData: fish.extend({}),
        events: {
            'click #datarecovery-search-btn': 'search'
        },

        serialize: function() {
            return this.i18nData;
        },

        afterRender: function() {
            var me = this;
            //初始化所属系统
            me.initNodeIdTree();
            me.initStartAndEndTime();
        },

        initNodeIdTree: function() {
            // 所属系统，下拉树
            var nodeId_setting = {
                async: {
                    enable: true,
                    contentType: "application/json",
                    url: webRoot + 'common/sysModule/queryAllForTree.do',
        //          url: 'nodes.jsp',
                    type: 'post'
                },
                view: {
                    dblClickExpand:false,
                    selectedMulti: false //如果为true,按住ctrl可以多选，false为单选 
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "pId",
                        rootPId: 0,
                        iconSkin:"icon01"
                    }
                },
                callback: {
                    onClick: nodeIdonClick
                }
            };
            
            var nodeIdTree = $.fn.zTree.init($("#nodeId-tree-datarecovery-search"), nodeId_setting);
            var nodeIdTreeObj = $.fn.zTree.getZTreeObj("nodeId-tree-datarecovery-search");
            function nodeIdonClick(){
                nodes = nodeIdTree.getSelectedNodes();
                var nodeIdInput = $('input[name="nodeId"]');
                // 设置控件的其他属性：vId，为真正的value
                nodeIdInput.attr("vId",nodes[0].id).val(nodes[0].name);
                hideNodeIdTree();
            };
            //下拉树相对位置，随着窗口联动
            function nodeIdTreePos(){
                var inputObj = $('input[name="nodeId"]');
                var inputObjOffSet = $('input[name="nodeId"]').offset();
                    var $treePs = $("#nodeId-commbox-tree-datarecovery-search").css({left:inputObjOffSet.left + "px", top:inputObjOffSet.top + inputObj.outerHeight() + "px",width:inputObj.outerWidth() + "px",'z-index':9999});
                    return $treePs;
                    }
            $('input[name="nodeId"]').click(function(){
                nodeIdTreePos().slideDown("fast");
                $(window).resize(function(){
                    nodeIdTreePos();
                });
               $("body").bind("mousedown", onBodyDownNode);
            });
            function onBodyDownNode(event) {
                if (!(event.target.name == "nodeId" || event.target.id == "nodeId-commbox-tree-datarecovery-search" || $(event.target).parents("#nodeId-commbox-tree-datarecovery-search").length>0)) {
                    hideNodeIdTree();
                }
            };
            function hideNodeIdTree(){
                $("#nodeId-commbox-tree-datarecovery-search").fadeOut("fast");
                $("body").unbind("mousedown", onBodyDownNode);
            };
        },

        initStartAndEndTime: function(){
            $('#startTime-datarecovery-search').datetimepicker({
                viewType: "date",
                orientation:{ x: 'left', y: 'bottom' },
                todayBtn: true
            });
            $('#endTime-datarecovery-search').datetimepicker({
                viewType: "date",
                orientation:{ x: 'left', y: 'bottom' },
                todayBtn: true
            });
        },

        search: function(){
            queryParam = $('.searchform-datarecovery').form('value'); //序列化表格内容为字符串
            queryParam.nodeId = $('#nodeId-datarecovery-search').attr('vId');
            if ($(".searchform-datarecovery").isValid()) {
                fish.trigger('DATARECOVERY_SERACH', queryParam);
            }
        }
    });
});
